/* 
 * Created on 2008-1-15
 * Last modified on 2009-12-9
 * Powered by dev.yeqiangwei.com
 */
package com.yeqiangwei.commons.util;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author HongGuang
 */
public class Constants {

	private static final Log log = LogFactory.getLog(Constants.class);
	public static final String DEFAULT_CHARSET = "UTF-8";
	public static String ROOT_PATH = null;
	public static String PARENT_PATH = null;
	public static String CLASS_PATH = null;
	public static String WEB_INF_PATH = null;
	public static String UTF_8 = "UTF-8";
	public static String GBK = "GBK";
	public static String ISO_8895_1 = "ISO-8859-1";

	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		} catch (Throwable ex) {
			log.error(
					"Cannot access thread context ClassLoader - falling back to system class loader",
					ex);
		}
		return cl;
	}

	static {
		if (ROOT_PATH == null) {
			if (ROOT_PATH == null) {
				String tmp = Constants.class.getProtectionDomain()
						.getCodeSource().getLocation().getPath();
				System.out
						.println("Constants...getCodeSource().getLocation(): "
								+ Constants.class.getProtectionDomain()
										.getCodeSource().getLocation());

				System.out
						.println("Constants...getCodeSource().getLocation().getPath(): "
								+ Constants.class.getProtectionDomain()
										.getCodeSource().getLocation()
										.getPath());

				String separator = File.separator;

				if (System.getProperty("os.name").toLowerCase()
						.indexOf("windows") != -1) {
					separator = "/";
					if (tmp.startsWith("file:/")) {
						tmp = tmp.replace("file:/", "");
					} else if (tmp.startsWith("/") && tmp.length() > 1) {
						tmp = tmp.substring(1, tmp.length());
					}
				}
				int i = tmp.indexOf("classes");
				int n = tmp.indexOf(".jar");
				System.out.println("os.name: " + System.getProperty("os.name"));
				System.out.println("tmp(1): " + tmp);
				System.out.println("File.separator: " + separator);
				if (i != -1) {
					ROOT_PATH = tmp.substring(0, i);
				} else if (n != -1) {
					ROOT_PATH = tmp
							.substring(0, tmp.lastIndexOf(separator) + 1);
				} else {
					ROOT_PATH = tmp;
				}
				System.out.println("ROOT_PATH(1): " + ROOT_PATH);
				if (ROOT_PATH.endsWith("lib" + separator)) {
					ROOT_PATH = ROOT_PATH.substring(0, ROOT_PATH.length() - 4);
				} else if (ROOT_PATH.endsWith("lib")) {
					ROOT_PATH = ROOT_PATH.substring(0, ROOT_PATH.length() - 3);
				}
				System.out.println("ROOT_PATH(2): " + ROOT_PATH);
				PARENT_PATH = new File(ROOT_PATH).getParent() + separator;
				if (CLASS_PATH == null) {
					CLASS_PATH = System.getProperty("java.class.path");
				}
				WEB_INF_PATH = ROOT_PATH + "classes" + separator;
				System.out.println("Constants#ROOT_PATH: " + ROOT_PATH
						+ ", PARENT_PATH: " + PARENT_PATH + ", CLASS_PATH: "
						+ CLASS_PATH);
			}
		}
	}

	public static void main(String args[]) {
		
	}
	

}
